﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;
using MOIS;

namespace WastedMechanics
{
    public class MouseInput
    {
        public Vector2 Position;
        public Vector2 Delta;
        public float Speed;
        public int ScrollAmount;
        
        private bool[] _buttonDown;
        private bool[] _buttonPressed;
        private bool[] _buttonReleased;

        public MouseInput()
        {
            Position = new Vector2(0.5f, 0.5f);
            Delta = new Vector2();
            Speed = 0.0015f;

            int buttonsCount = Enum.GetNames(typeof(MouseButtonID)).Length;            
            _buttonDown = new bool[buttonsCount];
            _buttonPressed = new bool[buttonsCount];
            _buttonReleased = new bool[buttonsCount];
            
            Engine.Mouse.MousePressed += MousePressed;
            Engine.Mouse.MouseReleased += MouseReleased;            
        }        

        bool MousePressed(MouseEvent arg, MouseButtonID id)
        {
            _buttonDown[(int)id] = true;
            _buttonPressed[(int)id] = true;            
            return true;
        }

        bool MouseReleased(MouseEvent arg, MouseButtonID id)
        {
            _buttonDown[(int)id] = false;
            _buttonReleased[(int)id] = true; 
            return true;
        }

       

        public void Update()
        {            
            Delta.x = (float)(Engine.Mouse.MouseState.X.rel)/Engine.RenderWindow.Width;
            Delta.y = (float)(Engine.Mouse.MouseState.Y.rel)/Engine.RenderWindow.Height;
            Delta *= Speed * (float)Engine.RenderWindow.Width;
            Position.x += Delta.x;
            Position.y += Delta.y;

            Position.x = Helper.Clamp(0, 1, Position.x);
            Position.y = Helper.Clamp(0, 1, Position.y);

            ScrollAmount = Engine.Mouse.MouseState.Z.rel;
        }

        public void ResetClicks()
        {
            for (int i = 0; i < _buttonPressed.Length; i++)
            {
                _buttonPressed[i] = false;
                _buttonReleased[i] = false;
            }
            ScrollAmount = 0;
        }

        public bool ButtonDown(MouseButtonID id)
        {
            return _buttonDown[(int)id];
        }

        public bool ButtonPressed(MouseButtonID id)
        {
            return _buttonPressed[(int)id];
        }

        public bool ButtonReleased(MouseButtonID id)
        {
            return _buttonReleased[(int)id];
        }
    }
}
